package vip.xiaonuo.smzq.modular.psba.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.modular.kcl.service.IZyclService;
import vip.xiaonuo.smzq.modular.psba.entity.GtWjglb;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaSqjlCzrz;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaSqjlJbqk;
import vip.xiaonuo.smzq.modular.psba.entity.param.PsbaApprove;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaSqjlCzrzVo;
import vip.xiaonuo.smzq.modular.psba.mapper.GtWjglbMapper;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaSqjlCzrzMapper;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaSqjlJbqkMapper;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaSqjlYfjbqkMapper;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaSqjlCzrzService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.user.entity.Role;
import vip.xiaonuo.smzq.modular.user.entity.User;
import vip.xiaonuo.smzq.modular.user.mapper.RoleMapper;
import vip.xiaonuo.smzq.modular.user.mapper.UserMapper;
import vip.xiaonuo.sys.modular.role.enums.SysRoleEnum;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 评审备案申请记录-操作日志 服务实现类
 *
 * @author Kevin
 * @since 2024-06-11
 */
@Service
public class PsbaSqjlCzrzServiceImpl extends ServiceImpl<PsbaSqjlCzrzMapper, PsbaSqjlCzrz> implements IPsbaSqjlCzrzService {

    @Resource
    private PsbaSqjlCzrzMapper psbaSqjlCzrzMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Resource
    private GtWjglbMapper gtWjglbMapper;//文件关联

    //资源储量-年报
    @Resource
    private IZyclService iZyclService;

    /**
     * 添加评审备案操作日志
     */
    @Override
    public void addRecord(PsbaApprove psbaApprove) {
        //获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        Role role = roleMapper.selectById(user.getPositionId());

        //根据用户的角色进行审批
        PsbaSqjlCzrz czrz = new PsbaSqjlCzrz();
        czrz.setPsbajlId(psbaApprove.getPsbabh());
        czrz.setCzrid(user.getId());
        czrz.setCzrxm(user.getName());
        czrz.setCzsj(DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss"));
        czrz.setYj(psbaApprove.getRemark());
        czrz.setFjIds(psbaApprove.getFileIds());
        czrz.setCzlx(psbaApprove.getType());

        //审批完成需要同步评审备案数据到年报储量，年报报告 以及生命周期
        if (psbaApprove.getType().equals("7")){
            iZyclService.synchronizationZycl(psbaApprove.getPsbabh(),czrz.getCzsj());
        }

//        if (SysRoleEnum.XIANSH.getValue().equals(role.getCode())) {
//            czrz.setCzlx("2");
//        } else if (SysRoleEnum.KYQR.getValue().equals(role.getCode())) {
//            czrz.setCzlx("6");
//        }
        this.save(czrz);
    }

    /**
     * 创建评审备案申请时，添加评审备案操作日志
     */
    public void addRecordApply(String psbabh) {
        //获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());

        //根据用户的角色进行审批
        PsbaSqjlCzrz czrz = new PsbaSqjlCzrz();
        czrz.setPsbajlId(psbabh);
        czrz.setCzrid(user.getId());
        czrz.setCzrxm(user.getName());
        czrz.setCzsj(DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss"));
        czrz.setCzlx("1");
        czrz.setBz("评审备案申请");
        this.save(czrz);
    }

    @Override
    public Page<PsbaSqjlCzrz> page(PsbaSqjlCzrz psbaSqjlCzrz) {
        QueryWrapper<PsbaSqjlCzrz> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }
    /**
     * 评审备案申请记录-操作日志节点详情
     * @param psbabh 评审备案编号
     */
    @Override
    public List<PsbaSqjlCzrzVo> getPsbaSqjlCzrzList(String psbabh) {
        List<PsbaSqjlCzrzVo> volist =new ArrayList<>();
        QueryWrapper<PsbaSqjlCzrz> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("psbajl_id",psbabh);
        queryWrapper.orderByAsc("czlx");
        List<PsbaSqjlCzrz> list = psbaSqjlCzrzMapper.selectList(queryWrapper);
        for (PsbaSqjlCzrz psbaSqjlCzrz:list) {
            PsbaSqjlCzrzVo psbaSqjlCzrzVo=new PsbaSqjlCzrzVo();
            BeanUtil.copyProperties(psbaSqjlCzrz, psbaSqjlCzrzVo);
            if (psbaSqjlCzrz.getFjIds()!=null&&psbaSqjlCzrz.getFjIds().length()>0){
                List<String> fileIds = Arrays.stream(psbaSqjlCzrz.getFjIds().split(","))
                        .collect(Collectors.toList());
                QueryWrapper<GtWjglb> queryWrapperwj = new QueryWrapper<>();
                queryWrapperwj.in("pkid",fileIds);
                List<GtWjglb> listwj = gtWjglbMapper.selectList(queryWrapperwj);
                psbaSqjlCzrzVo.setGtWjglbList(listwj);
            }
            volist.add(psbaSqjlCzrzVo);
        }
        return volist;
    }
}
